package org.soarsky.wealthchengdu.google.map;



import org.soarsky.wealthchengdu.R;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Point;
import android.util.Log;

public class LocationGoogleOverlays extends Overlay implements LocationOverlaysInterface{
	private int resouseId[] = { R.drawable.icon_loc_light,
			R.drawable.icon_loc_normal, R.drawable.icon_locr_light,
			R.drawable.icon_locr_normal };
	private Bitmap locationBitmap = null;
	private Bitmap bitmapArray[] = null;
	private Bitmap bitmapRotateNor,bitmapRotatePress = null;
	private OverlayCallBack callBack = null;
	private LocationPoint location  =null;
	private Point point = new Point();
	private Projection projection = null;
	private MapController controller = null;
	private int bitmap_w,bitmap_h;
	private boolean focus = false;
	private float currentBearing = -1;
	private boolean changeBraring = false;
	private boolean arrowchange = false;
	private Matrix vMatrix = null;
	private Paint fillStrokePaint = null,emptyStrockePaint = null,bitmapPaint = null;
	private Context context= null;
	private GeoPoint geoPoint  = null;
	private MapView mapView = null;
	public LocationGoogleOverlays(Context context,MapView mapView) {
		this.context = context;
		this.mapView = mapView;
		controller = mapView.getController();
		projection = mapView.getProjection();
		init();
	}
	private void init(){
		//pointContorl = new PointContorl();
		
		geoPoint = new GeoPoint((int) (30.6750701847006 * 1E6),(int) (104.004041925034 * 1E6));
		fillStrokePaint = new Paint();
		fillStrokePaint.setARGB(10, 99, 147, 236);
		fillStrokePaint.setStyle(Paint.Style.FILL_AND_STROKE);
		fillStrokePaint.setStrokeWidth(2);
		fillStrokePaint.setAntiAlias(true);
		emptyStrockePaint = new Paint();
		emptyStrockePaint.setColor(Color.argb(255, 99, 147, 236));
		emptyStrockePaint.setStyle(Paint.Style.STROKE);
		emptyStrockePaint.setStrokeWidth(1);
		emptyStrockePaint.setAntiAlias(true);
		bitmapPaint = new Paint();
		bitmapPaint.setAntiAlias(true);
		vMatrix = new Matrix();
		int lenght = resouseId.length;
		bitmapArray = new Bitmap[lenght];
		for (int i = 0; i < lenght; i++) {
			bitmapArray[i] = BitmapFactory.decodeResource(
					context.getResources(), resouseId[i]);
		}
		locationBitmap = bitmapArray[3];
		bitmap_w = locationBitmap.getWidth();
		bitmap_h  = locationBitmap.getHeight();
	}
	public GeoPoint createGeoPoint(double latitude,double longitude){
		return new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
	}
	@Override
	public boolean onTap(GeoPoint p, MapView mapView) {
		// TODO Auto-generated method stub
		if(null==callBack)return false;
		if (null != projection && null != point) {
			Point out = new Point();
			projection.toPixels(p, out);
			if ((out.x >= (point.x - bitmap_w) && out.x <= (point.x + bitmap_w))
					&& (out.y >= (point.y - bitmap_h) && out.y <= (point.y + bitmap_h))) {
				setOnFocus(true);
				return getOnFocus();
			} else {	
			}
			out = null;
		}
		if(focus){
			focus  =false;
			callBack.onLostFocus();
		}

		return super.onTap(p, mapView);
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		//super.draw(canvas, mapView, shadow);
		if(null==geoPoint||null==location)return;
		if(null==projection)projection = mapView.getProjection();
		projection.toPixels(geoPoint, point);

		canvas.save();
		canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG
				| Paint.FILTER_BITMAP_FLAG));
		projection.toPixels(geoPoint, point);
		if(location.getAccuracy()<1000&&location.hasAccuracy()){
			float radius = projection.metersToEquatorPixels(location.getAccuracy());
			canvas.drawCircle(point.x, point.y, radius, fillStrokePaint);
			canvas.drawCircle(point.x, point.y, radius, emptyStrockePaint);
		}
		if(currentBearing>-1){
			if(changeBraring){
				changeBraring = false;
				//currentBearing = location.getBearing();
				recycleBitmapRotate();
				vMatrix.setRotate(currentBearing);
				if(bitmapRotateNor==null)
					bitmapRotateNor = Bitmap.createBitmap( bitmapArray[0], 0, 0,  bitmapArray[0].getWidth() // ���
						,  bitmapArray[0].getHeight() 
						, vMatrix, true);
				if(bitmapRotatePress==null)
					bitmapRotatePress = Bitmap.createBitmap( bitmapArray[1], 0, 0,  bitmapArray[1].getWidth() // ���
								,  bitmapArray[1].getHeight() 
								, vMatrix, true);
			}
			if(bitmapRotateNor==null||bitmapRotatePress==null){
				locationBitmap = arrowchange ?  bitmapArray[0] : bitmapArray[1];
			}else {
				locationBitmap = arrowchange ? bitmapRotateNor :bitmapRotatePress;
			}
		}else {
			locationBitmap = arrowchange ?  bitmapArray[2] : bitmapArray[3];
		}
		canvas.drawBitmap(locationBitmap, point.x - locationBitmap.getWidth() / 2, point.y
				- locationBitmap.getHeight() / 2, bitmapPaint);
		canvas.restore();
		
		
	}
	private void recycleBitmapRotate(){
		if(null!=bitmapRotateNor){
			if(bitmapRotateNor.isRecycled())
				bitmapRotateNor.recycle();
			bitmapRotateNor = null;
		}
		if(null!=bitmapRotatePress){
			if(bitmapRotatePress.isRecycled())
				bitmapRotatePress.recycle();
			bitmapRotatePress = null;
		}
		
	}
	public void locationChange(LocationPoint location) {
		// TODO Auto-generated method stub
		this.location = location;
		if(null!=geoPoint)geoPoint=null;
		geoPoint = createGeoPoint(location.getLatitude(),location.getLongitude());
		
		//MapConstant.log("locationChange->"+location);
		
	}
	public void locationChange(LocationPoint location, boolean isInvalidate) {
		// TODO Auto-generated method stub
		locationChange(location);
		if(mapView!=null&&isInvalidate)
			mapView.invalidate();
	}
	
	public void animateTo() {
		// TODO Auto-generated method stub
		if(null!=controller&&null!=geoPoint) {
			controller.animateTo(geoPoint);
		}
		
	}
	
	public void animateTo(int x, int y) {
		// TODO Auto-generated method stub
		if(null!=controller&&null!=geoPoint&&null!=point){
			projection.toPixels(geoPoint, point);
			controller.animateTo(projection.fromPixels(point.x+x,point.y+y));
		}
	}
	
	public void setCenter() {
		// TODO Auto-generated method stub
		if(null!=controller&&null!=geoPoint) {
			controller.setCenter(geoPoint);
		}
	}
	
	public void setCenter(int x, int y) {
		// TODO Auto-generated method stub
		if(null!=controller&&null!=geoPoint&&null!=point){
			projection.toPixels(geoPoint, point);
			controller.animateTo(projection.fromPixels(point.x+x,point.y+y));
		}
	}
	
	
	public void setOnFocus(boolean focus) {
		// TODO Auto-generated method stub
		this.focus = focus;
		if(focus) {
			callBack.onGotFocus(location);
			if(MapOverlaysControlFactory.getInstance().getMarkOverlays()!=null&&
					MapOverlaysControlFactory.getInstance().getMarkOverlays().getOnFocus())
			{
				MapOverlaysControlFactory.getInstance().getMarkOverlays().setOnFocus(-1,false);
			}
		}else {
			callBack.onLostFocus();
		}
	}
	
	public boolean getOnFocus() {
		// TODO Auto-generated method stub
		return focus;
	}
	
	
	public boolean disableLocation() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean enableLocation() {
		// TODO Auto-generated method stub
		return false;
	}
	public void setLcationBearing(float bearing) {
		// TODO Auto-generated method stub
		currentBearing = bearing;
		changeBraring = true;
	}

	public void setOnTapCallBack(OverlayCallBack callBack) {
		// TODO Auto-generated method stub
		this.callBack = callBack;
	}

	public void freeLocation() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public GeoPoint getGeoPoint() {
		// TODO Auto-generated method stub
		return geoPoint;
	}
	@Override
	public void createGeoPoint(LocationPoint location) {
		// TODO Auto-generated method stub
		createGeoPoint(location.getLatitude(),location.getLongitude());
	}
	
	
	

}
